<?php
namespace admin\controllers;


use admin\models\Relation;
use admin\models\UserFamily;
use admin\models\UserInfo;
use Yii;
use yii\data\Pagination;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;

/**
 * Created by PhpStorm.
 * User: 沈小鱼
 * Date: 2016/7/26
 * Time: 19:37
 */
class FamilyController extends BaseController{



    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['add','list','del'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                ],
            ],
        ];
    }



    public function actionList(){
        $user_id = Yii::$app->request->get('user_id');
        $data = UserFamily::find()->where('mu_id='.$user_id);
        $familyPage = new Pagination([
            'totalCount' =>$data->count(),
            'pageSize' => '5',
            'pageSizeParam' => false,
        ]);
        $familys = $data->offset($familyPage->offset)->limit($familyPage->limit)->all();
        $relation = Relation::getRelations();
        return $this->renderPartial('list',[
            'familyPage'=>$familyPage,
            'familys'=>$familys,
            'relation'=>$relation,
        ]);
    }


    public function actionAdd(){
        $admin = Yii::$app->user->identity->getId();
        if(empty($admin)){
            return $this->showResult(302,'用户已登出');
        }
        $user_id = Yii::$app->request->get('id');
        if(empty($user_id)){
            return $this->showResult(301,'读取用户信息失败');
        }
        $familyArr = Yii::$app->request->post('UserFamily');
        if(empty($familyArr['mf_id'])){
            $family = new UserFamily();
        }else{
            $family = UserFamily::findOne($familyArr['mf_id']);
        }
        $family->attributes = $familyArr;
        $family->mu_id = $user_id;
        if($family->validate()){
            if(!$family->save()){
                return $this->showResult(400,'保存失败');
            }else{
                return $this->showResult(200,'保存成功');
            }
        }else{
            return $this->showResult(300,array_values($family->getFirstErrors())[0]);
        }
    }

    public function actionDel(){
        $mf_id = Yii::$app->request->post('mf_id');
        if(empty($mf_id)){
            return $this->showResult(301,'未获取到病历信息');
        }
        $admin = Yii::$app->user->identity;
        if(empty($admin)){
            return $this->showResult(302,'您的登录信息已失效，请重新登录');
        }
        $family = UserFamily::findOne($mf_id);
        if(empty($family)){
            return $this->showResult(301,'该条记录已不存在');
        }
        $row = Yii::$app->db->createCommand('delete from mm_user_family WHERE mf_id='.$mf_id)->execute();
        if(!empty($row)){
            return $this->showResult(200,'删除成功');
        }else{
            return $this->showResult(400,'删除失败');
        }
    }

}